package Greed;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//合并区间
public class MergeIntervals {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals,(v1,v2)->{
            return v1[0]-v2[0];
        });
        int left=intervals[0][0];
        int right=intervals[0][1];
        List<int[]> ret=new ArrayList<>();
        for (int i = 1; i < intervals.length; i++) {
            int a=intervals[i][0];
            int b=intervals[i][1];
            if(a<=right){
                right=Math.max(right,b);
            }else {
                ret.add(new int[]{left,right});
                left=a;
                right=b;
            }
        }
        ret.add(new int[]{left,right});
        return ret.toArray(new int[0][]);
    }
}
